package com.gu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gu.entity.EduTeacher;
import com.gu.entity.vo.TeacherQuery;
import com.boot.exception.DiyException;
import com.gu.service.EduTeacherService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-11-19:44
 */
@Api(tags = "讲师管理")
@RequestMapping("/eduService")
@RestController
@CrossOrigin   //springboot 自带跨越注解
public class EduTeacherController {

    @Resource
    private EduTeacherService teacherService;

    @ApiOperation(value = "获取老师列表")
    @GetMapping("/getAll")
    public CommonResult getAllTeachers() {
        List<EduTeacher> list = teacherService.list();
        if (list == null) {
            return CommonResult.error();
        } else {
            return CommonResult.success().data("items", list);
        }
    }

    @ApiOperation(value = "根据id查询教师列表")
    @GetMapping("/getTeacherById/{id}")
    public CommonResult getTeacherById(@PathVariable("id")String id) {
//        String str = String.valueOf(id);
        EduTeacher teacher = teacherService.getById(id);
        if (StringUtils.isEmpty(teacher)) {
            return CommonResult.error();
        } else {
            return CommonResult.success().data("items", teacher);
        }
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteTeacher(@PathVariable("id") String id) {
        boolean result = teacherService.removeById(id);
        return result ? CommonResult.success() : CommonResult.error();
    }

    //分页 条件查询 列表 @RequestBody json对象封装需要用post请求
    @ApiOperation(value = "获取老师分页列表")
    @PostMapping("/getTeacherPage/{current}/{limit}")
    public CommonResult getTeacherPage(@PathVariable("current") Long current,
                                       @PathVariable("limit") Long limit,
                                       @RequestBody(required = false)TeacherQuery teacherQuery) {
        Boolean switch1 = true;
        if (current <= 0 && limit <= 0 && teacherQuery ==null) {
            switch1 = false;
        }
        Page<EduTeacher> pageParam = new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        //便于管理员的显示功能，让其按照创建时间降序排列
        queryWrapper.orderByDesc("gmt_create");

        //会查询结果记录在pageParam中
        teacherService.page(pageParam,queryWrapper);
        if (pageParam == null) {
            switch1 = false;
        }
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return switch1 ? CommonResult.success().data("rows", total).data("items", records) : CommonResult.error();

    }

    @ApiOperation(value = "新增教师")
    @PostMapping("/insertTeacher")
    public CommonResult insertTeacher(@RequestBody EduTeacher teacher) {
        Boolean switch1 = true;
        boolean save = teacherService.save(teacher);
        return save ? CommonResult.success() : CommonResult.error();
    }

    @ApiOperation(value = "根据Id值修改教师信息")
    @PutMapping("/updateTeacher/{id}")
    public CommonResult updateTeacher(
            @PathVariable("id") String id,
            @RequestBody EduTeacher teacher) {
        //存在乐观锁，修改数据库表时候需要先查询，到缓存中
         teacherService.getById(id);
        teacher.setId(id);
        boolean result = teacherService.updateById(teacher);
        return result ? CommonResult.success() : CommonResult.error();
    }

    @ApiOperation(value = "异常测试类")
    @GetMapping("/exceptionTest")
    public CommonResult exceptionTest(){
        try {
            int i = 10/0 ;
        }catch (Exception e){
            throw new DiyException("444","自定义异常错误！");
        }
        return CommonResult.error();
    }


}
